package spider

import (
	"Spider/channel"
	"github.com/PuerkitoBio/goquery"
	"fmt"
	"log"
	"strings"
	"Spider/stru"
	"os"
	"net/http"
	"io"
)

type Worker interface {
	MakeUrls()
	Query()
	Download()
	//Save()bool
	Show()
}

type JokeSpider struct {
	
}

type PicSpider struct {

}



func (self *JokeSpider)MakeUrls(){
	cat:="http://www.qiushibaike.com/text/"
	channel.UrlChan<-cat
	defer close(channel.UrlChan)
	channel.Sync1<- struct{}{}
}

func(self *JokeSpider)Query(){
	<-channel.Sync1
	for page:=range channel.UrlChan{
		doc, err := goquery.NewDocument(page)
		if err != nil {
			log.Fatal(err)
			return
		}//#content也可以使用
		doc.Find(".content").Each(func(i int, s *goquery.Selection) {
			//fmt.Println(s.Text())
			//value := s.Find(".content")
			index := s.Find("span").Text()
			//fmt.Println(index)
			channel.Jokes <- index
		})
	}
	defer close(channel.Jokes)
	channel.Sync2<- struct{}{}
}

func(self *JokeSpider)Show(){
	for elem:=range channel.Jokes{
		fmt.Println(elem)
	}
	channel.Sync2<- struct{}{}
}

func (self*JokeSpider)Download(){

}

func (self *PicSpider)MakeUrls(){
	cat:="https://www.qiushibaike.com/imgrank/"
	channel.UrlChan<-cat
	defer close(channel.UrlChan)
	channel.Sync1<- struct{}{}
}

func(self *PicSpider)Query(){
	<-channel.Sync1
	var pic stru.Picture
	for page:=range channel.UrlChan{
		doc, err := goquery.NewDocument(page)
		if err != nil {
			log.Fatal(err)
			return
		}
		doc.Find(".thumb").Each(func(i int, s *goquery.Selection) {
			//fmt.Println(s.Text())
			 value:= s.Find(".illustration")
			 title,_:=value.Attr("alt")
			 src,_:=value.Attr("src")
			//index:=value.Find("[href]")
			//index := value.Find("img src").Text()
			////fmt.Println(index)
			//channel.Jokes <-index
			pic.Name=title
			//in:=strings.Index(src,".")
			pic.Url=src
			in:=strings.LastIndex(src,".")
			pic.Ext=src[in:]
			channel.PicChan<-pic
		})
	}
	defer close(channel.Jokes)
	defer close(channel.PicChan)
	channel.Sync2<- struct{}{}
}

func(self *PicSpider)Show(){
	for elem:=range channel.PicChan{
		fmt.Printf("%+v\n",elem)
	}
	channel.Sync2<- struct{}{}
}

func(self *PicSpider)Download(){
	const dir="./Download/"
	for elem:=range channel.PicChan{
		cat :="http:"+elem.Url
		filepath:=dir+elem.Name+elem.Ext
		basepath,err:=os.Stat(dir)
		if err!=nil||basepath.IsDir()==false{
			err=os.Mkdir(dir,os.ModePerm)
			if err!=nil{
				fmt.Println("create dirctory failed!",err)
			}
		}
		data,err:=http.Get(cat)
		if err!=nil{
			log.Fatal(err)
			return
		}
		f,err:=os.Create(filepath)
		if err!=nil{
			panic(err)
		}
		io.Copy(f,data.Body)

	}
	channel.Sync2<- struct{}{}
}
